﻿Imports ASAP.InterfazNegocio

Public Class wTeorias

    Public Teoria As TiposDatosSTE.Teoria

#Region "Eventos"

    Private Sub TiposErrores_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Space Then
            Aceptar()
        ElseIf e.Key = Key.Enter Then
            Aceptar()
        ElseIf e.Key = Key.Escape Then
            Cancelar()
        Else

            Dim c As String = ""

            Select Case e.Key
                Case Key.A
                    c = "a"
                Case Key.C
                    c = "c"
                Case Key.F
                    c = "f"
                Case Key.G
                    c = "g"
                Case Key.L
                    c = "l"
                Case Key.O
                    c = "o"
                Case Key.P
                    c = "p"
                Case Key.S
                    c = "s"
                Case Key.T
                    c = "t"
                Case Key.V
                    c = "v"
            End Select
            If c <> String.Empty Then
                SeleccionaItem(c)
            End If

        End If
    End Sub

    Private Sub SeleccionaItem(ByVal c As String)


        Dim firstItem As TiposDatosSTE.Teoria = CType(lbTeorias.SelectedItem, TiposDatosSTE.Teoria)
        Dim secondItem As TiposDatosSTE.Teoria = CType(lbTeorias.SelectedItem, TiposDatosSTE.Teoria)

        If (firstItem IsNot Nothing) Then
            If firstItem.TituloTeoria.ToLower.StartsWith(c) Then

                If lbTeorias.SelectedIndex < lbTeorias.Items.Count - 1 Then
                    secondItem = CType(lbTeorias.Items(lbTeorias.SelectedIndex + 1), TiposDatosSTE.Teoria)
                    If secondItem.TituloTeoria.ToLower.StartsWith(c) Then
                        lbTeorias.SelectedItem = secondItem
                        Exit Sub
                    End If
                End If

            End If
        End If


        For Each item As TiposDatosSTE.Teoria In lbTeorias.Items
            If item.TituloTeoria.ToLower.StartsWith(c) Then
                lbTeorias.SelectedItem = item
                Exit Sub
            End If
        Next

    End Sub

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Me.lbTeorias.ItemsSource = Agente.DameTeorias
        Me.lbTeorias.Focus()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAceptar.Click
        Try
            Aceptar()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancelar.Click
        Try
            Cancelar()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub btnVer_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnVer.Click
        Try
            VerDocumento()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnGuardar.Click
        Try
            GuardarDocumento()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub lbTeorias_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lbTeorias.MouseDoubleClick
        Try
            Aceptar()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

#End Region

    Private Sub Aceptar()
        If lbTeorias.SelectedItem IsNot Nothing Then
            Dim te As TiposDatosSTE.Teoria = CType(lbTeorias.SelectedItem, TiposDatosSTE.Teoria)
            Teoria = te
        Else
            Teoria = Nothing
        End If
        Me.Close()
    End Sub

    Private Sub Cancelar()
        Teoria = Nothing
        Me.Close()
    End Sub

    Private Sub VerDocumento()

        Dim pathTemporal As String = My.Application.Info.DirectoryPath & "\tmp"

        If Not IO.Directory.Exists(pathTemporal) Then
            IO.Directory.CreateDirectory(pathTemporal)
        End If

        If lbTeorias.SelectedItem IsNot Nothing Then
            Dim te As TiposDatosSTE.Teoria = CType(lbTeorias.SelectedItem, TiposDatosSTE.Teoria)
            Dim Path = pathTemporal & "\" & te.TituloTeoria & ".docx"
            My.Computer.FileSystem.WriteAllBytes(Path, te.Documento, False)
            System.Diagnostics.Process.Start(Path)
        End If


    End Sub

    Private Sub GuardarDocumento()

        Dim pathTemporal As String = My.Application.Info.DirectoryPath & "\tmp"

        If lbTeorias.SelectedItem IsNot Nothing Then
            Dim te As TiposDatosSTE.Teoria = CType(lbTeorias.SelectedItem, TiposDatosSTE.Teoria)
            Dim Path = pathTemporal & "\" & te.TituloTeoria & ".docx"

            If Not IO.File.Exists(Path) Then
                Throw New Exception("El siguiente fichero no existe: " & Path)
            Else
                te.Documento = My.Computer.FileSystem.ReadAllBytes(Path)
                InterfazNegocio.Agente.ActualizaTeoria(te)
                IO.File.Delete(Path)
            End If

        End If


    End Sub

End Class
